//
//  WebViewController.m
//  AmnesiaMagazine
//
//  Created by Vicente Vicens on 21/07/11.
//  Copyright 2011 Imathink World, S.L.N.E. All rights reserved.
//

#import "WebViewController.h"

@implementation WebViewController

@synthesize vvDelegate;
@synthesize mainToolbar;
@synthesize webView;
@synthesize doneButton;
@synthesize backButton;
@synthesize forwardButton;
@synthesize activity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    self.vvDelegate = nil;
    [mainToolbar release];
    [backButton release];
    [forwardButton release];
    [activity release];
    [doneButton release];
    [webView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.activity = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    CGRect frame = self.activity.bounds;
    frame.origin.x = self.mainToolbar.frame.size.width - frame.size.width - 120;
    frame.origin.y = (self.mainToolbar.frame.size.height - frame.size.height) / 2;
    self.activity.frame = frame;
    self.activity.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [self.mainToolbar addSubview:self.activity];
    self.webView.delegate = self;
    [self checkBarButtonsState];
        
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.kankiu.com"]]];
}

- (void)viewWillAppear:(BOOL)animated {
    self.webView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.webView stopLoading];
    self.webView.delegate = nil;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.webView = nil;
    self.doneButton = nil;
    self.mainToolbar = nil;
    self.backButton = nil;
    self.forwardButton = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

- (IBAction)showDoneButton:(id)sender {
    if ([self.webView isLoading]) {
        [self.webView stopLoading];
    }
    self.webView.delegate = nil;
    
    if ([self.vvDelegate respondsToSelector:@selector(webViewControllerWantsToClose:)]) {
        [self.vvDelegate webViewControllerWantsToClose:self];
    }
    //[self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.activity startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.activity stopAnimating];
    [self checkBarButtonsState];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.activity stopAnimating];
    [self checkBarButtonsState];
}

- (IBAction)goBackAction:(id)sender {
    [self.webView goBack];
    [self checkBarButtonsState];
}

- (IBAction)goForwardAction:(id)sender {
    [self.webView goForward];
    [self checkBarButtonsState];
}

- (void)checkBarButtonsState {
    if ([self.webView canGoBack]) {
		self.backButton.enabled = YES;
	} else {
		self.backButton.enabled = NO;
	}
	
	if ([self.webView canGoForward]) {
		self.forwardButton.enabled = YES;
	} else {
		self.forwardButton.enabled = NO;
	}
}

@end
